package cn.zjweu.service.impl;

import cn.zjweu.dao.DishDao;
import cn.zjweu.dao.impl.DishDaoImpl;
import cn.zjweu.domain.Dish;
import cn.zjweu.domain.PageBean;
import cn.zjweu.service.DishService;

import java.util.List;

public class DishServiceImpl implements DishService {

    private DishDao dishDao = new DishDaoImpl();

    @Override
    public PageBean<Dish> findDishPageByDishAndCategory(int currentPage, int pageSize, String dishName, String categoryName) {
        PageBean<Dish> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        int totalCount = dishDao.findCountByDishAndCategory(dishName, categoryName);
        pageBean.setTotalCount(totalCount);

        int start = (currentPage - 1) * pageSize;
        List<Dish> list = dishDao.findPageByDishAndCategory(start, pageSize, dishName, categoryName);
        for (Dish dish : list) {
            dish.setDishCategory(dishDao.findCategoryNameById(dish.getCategoryId()));
        }
        pageBean.setList(list);
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        pageBean.setTotalPage(totalPage);

        return pageBean;
    }

    @Override
    public Dish findDishById(int dishId) {
        return dishDao.findDishById(dishId);
    }
}
